package com.google.code.swingonfire.showcase.component.page;

import static com.google.code.swingonfire.builder.JComponentBuilders.*;

import java.awt.Color;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.google.code.eventsonfire.Events;
import com.google.code.eventsonfire.awt.AWTEvents;
import com.google.code.eventsonfire.swing.SwingEventHandler;
import com.google.code.swingonfire.component.ButtonPanel;
import com.google.code.swingonfire.component.page.JPageComponent;
import com.google.code.swingonfire.showcase.TitlePanel;

public class JPageComponentShowcase extends AbstractJPageComponentShowcase<JPageComponent>
{

    private static final long serialVersionUID = -1590937588165979408L;

    private int index = 0;

    public JPageComponentShowcase()
    {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected TitlePanel createTitlePanel()
    {
        return new TitlePanel(
            "JPageComponent",
            "A component, that displays another component. The displayed component can easily be changed. "
                + "The transition between components is animated.\n"
                + "The difference to the JPagePane is, that the JPageComponent just holds one component, the JPagePane holds a list of component.");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected JPageComponent createPageComponent()
    {
        JPageComponent result = new JPageComponent();

        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected JPanel createNavigationPanel()
    {
        JButton previousButton =
            Events.bind(buttonBuilder().text("Create Previous Component")
                .listener("previous", AWTEvents.fireOnAction()).opaque(false).build(), this);
        JButton nextButton =
            Events.bind(buttonBuilder().text("Create Next Component").listener("next", AWTEvents.fireOnAction())
                .opaque(false).build(), this);

        JPanel result = new ButtonPanel().left(previousButton).right(nextButton);

        result.setBorder(BorderFactory.createEmptyBorder(8, 12, 8, 12));
        result.setBackground(Color.WHITE);
        result.setOpaque(true);

        return result;
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "previous")
    public void onPrevioius()
    {
        getPageComponent().setPreviousComponent(new JPageSample(String.valueOf(index++)));
    }

    @SwingEventHandler(event = ActionEvent.class, eachTag = "next")
    public void onNext()
    {
        getPageComponent().setNextComponent(new JPageSample(String.valueOf(index++)));
    }

}
